package tq.com.tqteacher.mine.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqteacher.R;
import tq.com.tqteacher.mine.adapter.TQBuyInfoAdapter;
import tq.com.tqteacher.mine.model.TQBuyInfoSection;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.net.TQNetInterface;

public class TQBuyInfoFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private TextView mCourse;

    private TQBuyInfoAdapter mBuyInfoAdapter;
    private int year;

    private  int type = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View lView = inflater.inflate(R.layout.fragment_buyinfo_layout, container, false);
        mRecyclerView = lView.findViewById(R.id.recycleView);
        mCourse = lView.findViewById(R.id.course);

        if(mBuyInfoAdapter == null){
            mBuyInfoAdapter = new TQBuyInfoAdapter(getContext());
        }
        mRecyclerView.setAdapter(mBuyInfoAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

        initYear();
        getData();
        return lView;
    }

    public void setType(int t){
        type = t;
    }

    private void getData(){
        if(type > 0){
            TQNetInterface.getCardSelaInfo(TQLogin.getCurrent().getUserInfo().getClzid(), year, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQBuyInfoFragment.this.getActivity(), pNetResult.getErrMsg());
                        return;
                    }

                    dealResult(pNetResult.getData());
                }
            });
        }else{
            TQNetInterface.getCardActivityInfo(TQLogin.getCurrent().getUserInfo().getClzid(), year, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQBuyInfoFragment.this.getActivity(), pNetResult.getErrMsg());
                        return;
                    }

                    dealResult(pNetResult.getData());
                }
            });
        }

    }

    private void dealResult(JSONObject pObject){
        if(pObject == null)return;

        JSONObject lObject;
        if(type > 0){
            lObject = pObject.optJSONObject("sale");
        }else{
            lObject = pObject.optJSONObject("active");
        }
        if(lObject == null) return;

        String year = lObject.optString("year");

        TQBuyInfoSection lTQBuyInfoSection = new TQBuyInfoSection();

        TQBuyInfoSection.TQBuyInfoItem lInfoItem = new TQBuyInfoSection.TQBuyInfoItem();
        lInfoItem.setYear(year);
        lTQBuyInfoSection.setHeader(lInfoItem);

        JSONArray lArray;
        if(type > 0){
            lArray = lObject.optJSONArray("sale");
        }else {
            lArray = lObject.optJSONArray("active");
        }
        if(lArray == null)return;

        boolean isleft = true;
        for (int i = 0; i < lArray.length(); i++){
            try {
                JSONObject lObject1 = lArray.getJSONObject(i);

                lInfoItem = new TQBuyInfoSection.TQBuyInfoItem();
                lInfoItem.fromJson(lObject1);
                lInfoItem.setLeft(isleft);
                isleft = !isleft;

                lTQBuyInfoSection.addItem(lInfoItem);
            }catch (JSONException pE){
                pE.printStackTrace();
            }
        }

        if(lTQBuyInfoSection.getiItemCount() > 0){
            mBuyInfoAdapter.addSection(lTQBuyInfoSection);
        }
    }

    private void initYear(){
        SimpleDateFormat lDateFormat = new SimpleDateFormat("yyyy");
        String y = lDateFormat.format(new Date());

        year = Integer.valueOf(y);
    }
}
